//
//  BYLoginController.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/13.
//

#import "BYLoginController.h"
#import "BYLoginTextField.h"
#import "DBUser.h"

@interface BYLoginController ()<UITextFieldDelegate>

@property (nonatomic , strong) UIScrollView     *scrollView;
@property (nonatomic , strong) UILabel          *pageTitle;

@property (nonatomic , strong) BYLoginTextField *acountView;
@property (nonatomic , strong) BYLoginTextField *passwordView;
@property (nonatomic , strong) UIButton         *loginButton;
@property (nonatomic , strong) UIButton         *closedButton;

@end

@implementation BYLoginController

#pragma mark - LifeCycle

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.closedButton];
    [self.scrollView addSubview:self.pageTitle];
    [self.scrollView addSubview:self.acountView];
    [self.scrollView addSubview:self.passwordView];
    [self.scrollView addSubview:self.loginButton];
    
    [kNotificationCenterSington addObserver:self selector:@selector(loginSuccess) name:kFSNotificationNameLoginSuccess object:nil];
}

#pragma mark - NotificationEvent
- (void)loginSuccess {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Event

- (void)loginEvent {
    
    if (!ValidStr(self.acountView.text)) {
        [self.view makeToast:@"请输入用户名"];
        return;
    }
    
    if(!ValidStr(self.passwordView.text)) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    
    NSLog(@"登录操作-账号：%@ --- 密码：%@", self.acountView.text, self.passwordView.text);
    

    // 数据库查询验证
    [[DBUser sharedInstance] LinkDatabaseAndAddToQueue];
    [[DBUser sharedInstance] selectWithUser_Name:self.acountView.text WithUser_Password:self.passwordView.text];   
}

- (void)closeEvent {
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark - LazyGet

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.scrollEnabled = YES;
        _scrollView.userInteractionEnabled = YES;
    }
    return _scrollView;
}

- (UILabel *)pageTitle {
    if (!_pageTitle) {
        _pageTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, kAutoWithSize(40), kScreenWidth, kAutoWithSize(40))];
        _pageTitle.font = MZBoldFont(22);
        _pageTitle.textAlignment = NSTextAlignmentCenter;
        _pageTitle.text = @"登录";
    }
    return _pageTitle;
}


- (UIButton *)closedButton {
    if (!_closedButton) {
        _closedButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - kAutoWithSize(40), kAutoWithSize(40), kAutoWithSize(30), kAutoWithSize(30))];
        [_closedButton setTitle:@"X" forState:UIControlStateNormal];
        [_closedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_closedButton addTapAction:self selector:@selector(closeEvent)];
    }
    return _closedButton;
}

- (BYLoginTextField *)acountView {
    if (!_acountView) {
        _acountView = [[BYLoginTextField alloc]initWithFrame:CGRectMake(kAutoWithSize(30), kAutoWithSize(250), kScreenWidth - kAutoWithSize(60), kAutoWithSize(44))];
        _acountView.delegate = self;
        _acountView.font = MZFont(16);
        _acountView.limitTextLength = 20;
        _acountView.placeholder = @"请您输入账号或手机号";
        _acountView.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _acountView.clearButtonMode = UITextFieldViewModeWhileEditing;
        _acountView.text = @"13811112222";
    }
    return _acountView;
}

- (BYLoginTextField *)passwordView {
    if (!_passwordView) {
        _passwordView = [[BYLoginTextField alloc]initWithFrame:CGRectMake(kAutoWithSize(30), CGRectGetMaxY(self.acountView.frame) + kAutoWithSize(20), kScreenWidth - kAutoWithSize(60) - kAutoWithSize(26), kAutoWithSize(44))];
        _passwordView.secureTextEntry = YES;
        _passwordView.delegate = self;
        _passwordView.font = MZFont(16);
        _passwordView.limitTextLength = 20;
        _passwordView.placeholder = @"请输入密码";
        _passwordView.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _passwordView.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordView.text = @"123456";
       
    }
    return _passwordView;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [[UIButton alloc]initWithFrame:CGRectMake(kAutoWithSize(20), CGRectGetMaxY(self.passwordView.frame)+ kAutoWithSize(20) , kScreenWidth - kAutoWithSize(40), kAutoWithSize(45))];
        _loginButton.layer.masksToBounds = YES;
        _loginButton.layer.cornerRadius = kAutoWithSize(22.5);
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setBackgroundColor:UIColorFromRGB(0xFFA200) forState:UIControlStateDisabled];
        [_loginButton addTarget:self action:@selector(loginEvent) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateNormal];
        //        _loginButton.enabled = NO;
    }
    return _loginButton;
}


@end
